from airtest.core.api import keyevent, sleep, stop_app
from V1.air.air_core import AirClick


# 定义链接1方法
class Link1:
    # 实例化点击方法
    link1_click = AirClick()

    # 快手下单
    def PlaceOrder(self, poco):
        # 去看看
        self.link1_click.airClickByText(poco, '去看看')
        sleep(2)
        # 判断默认地址是否支持配送
        if self.link1_click.airImageExists('tpl1739936179356.png'):
            # 修改地址
            modifyAddress(poco)
        # 立即购买
        self.link1_click.airClickZuoBiao([921, 2330])
        sleep(1)
        # 取消优惠
        # cancelDiscount(poco)
        # 付款直接选花呗支付
        firstTokioPayment(poco)
        # 打开微信
        self.link1_click.airClickByText(poco, '微信')
        sleep(2)
        # 代付客服链接
        customerService(poco)
        sleep(2)
        # 关闭快手后台
        stop_app("com.smile.gifmaker")
        # 快手取消支付
        kwaiCancelPayment(poco)


# 定义链接2方法
class Link2:
    # 实例化点击方法
    link1_click = AirClick()

    # 快手下单
    def PlaceOrder(self, poco):
        # 查看商品详情
        self.link1_click.airClickByText(poco, '查看商品详情')
        sleep(2)
        # 判断默认地址是否支持配送
        if self.link1_click.airImageExists('tpl1739936179356.png'):
            # 修改地址
            modifyAddress(poco)
        # 立即购买
        self.link1_click.airClickZuoBiao([921, 2330])
        sleep(1)
        # 取消优惠
        # cancelDiscount(poco)
        # 付款直接选花呗支付
        firstTokioPayment(poco)
        # 打开微信
        self.link1_click.airClickByText(poco, '微信')
        sleep(2)
        # 代付客服链接
        customerService(poco)
        sleep(2)
        # 关闭快手后台
        stop_app("com.smile.gifmaker")
        # 快手取消支付
        kwaiCancelPayment(poco)


# 修改地址
def modifyAddress(poco):
    # 实例化点击方法
    address_click = AirClick()
    # 修改地址
    address_click.airClickByText(poco, '修改地址')
    sleep(2)
    address_click.airClickByText(poco, '未来城小区')
    sleep(2)


# 付款直接选花呗支付
def firstTokioPayment(poco):
    # 实例化点击方法
    first_tokio_click = AirClick()
    # 向上滑动屏幕
    first_tokio_click.airSwipScreen([550, 2147], [550, 1815])
    sleep(2)
    first_tokio_click.airClickByText(poco, '展开更多支付方式')
    sleep(2)
    first_tokio_click.airSwipScreen([550, 2147], [550, 1815])
    sleep(2)
    first_tokio_click.airClickByText(poco, '花呗支付')
    sleep(2)
    first_tokio_click.airClickZuoBiao([500, 2310])
    sleep(2)
    # 返回重选
    # first_tokio_click.airClickByText(poco, '返回重选')
    # sleep(1)
    # 展开选项
    first_tokio_click.airClickZuoBiao([535, 1886])
    sleep(2)
    # 找人代付
    first_tokio_click.airSwipScreen([535, 1939], [535, 771])
    sleep(2)
    first_tokio_click.airClickByText(poco, '找朋友帮忙付')
    sleep(1)
    # 复制链接
    first_tokio_click.airClickByText(poco, '复制链接')
    sleep(2)
    keyevent('HOME')
    sleep(2)


# 代付客服链接
def customerService(poco):
    # 实例化点击方法
    customerService_click = AirClick()
    keyevent('back')
    sleep(2)
    keyevent('back')
    sleep(2)
    # 客服链接
    customerService_click.airClickByText(poco, '代付客服链接')
    # customerService_click.airClickImage(filename='tpl1741159917926.png')
    # customerService_click.airClickZuoBiao([535, 2022])
    sleep(2)
    # 发送消息
    customerService_click.airClickZuoBiao([423, 2328])
    sleep(2)
    customerService_click.airClickZuoBiao([432, 1510])
    sleep(2)
    customerService_click.airClickZuoBiao([981, 1404])
    sleep(2)
    keyevent('back')


# 取消优惠
def cancelDiscount(poco):
    # 实例化点击方法
    cancelDiscount_click = AirClick()
    cancelDiscount_click.airClickByText(poco, '金币')
    sleep(2)
    cancelDiscount_click.airClickByText(poco, '金币抵钱')
    sleep(2)
    cancelDiscount_click.airClickByText(poco, '确定')
    sleep(2)


# 找人代付
def Payment(poco):
    # 实例化点击方法
    payment_click = AirClick()
    payment_click.airClickZuoBiao([536, 1942])
    sleep(2)
    payment_click.airSwipScreen([547, 1907], [547, 864])
    sleep(2)
    if payment_click.airImageExists('tpl1740108595083.png'):
        payment_click.airClickByText(poco, '找朋友帮忙付')
        sleep(2)
    else:
        # 花呗支付
        tokioPayment(poco)


# 快手取消支付
def kwaiCancelPayment(poco):
    # 实例化点击方法
    cancel_click = AirClick()
    keyevent('HOME')
    # 打开快手
    cancel_click.airClickByText(poco, '快手')
    sleep(2)
    # 取消支付
    keyevent('back')
    sleep(2)
    keyevent('back')
    sleep(2)
    keyevent('back')
    sleep(2)
    cancel_click.airClickByText(poco, '放弃')
    sleep(2)
    # 打开微信
    cancel_click.airClickByText(poco, '微信')
    sleep(2)
    keyevent('back')
    sleep(2)


# 取消支付，再花呗支付
def tokioPayment(poco):
    # 实例化点击方法
    tokio_click = AirClick()
    keyevent('back')
    sleep(2)
    keyevent('back')
    sleep(2)
    tokio_click.airClickByText(poco, '放弃')
    sleep(5)
    tokio_click.airClickByText(poco, '展开更多支付方式')
    sleep(2)
    tokio_click.airClickByText(poco, '花呗支付')
    sleep(2)
    tokio_click.airClickZuoBiao([524, 2200])
    sleep(2)
    tokio_click.airClickByText(poco, '返回重选')
    sleep(1)
    # 找人代付
    tokio_click.airSwipScreen([509, 2022], [509, 900])
    sleep(2)
    tokio_click.airClickByText(poco, '找朋友帮忙付')
    sleep(2)
